//
//  YCResetPasswordViewController.m
//  test
//
//  Created by 大锅 on 2017/7/13.
//  Copyright © 2017年 daguo. All rights reserved.
//

#import "YCResetPasswordViewController.h"
#import "YCLoginManager.h"
#import "AccountPasswordLoginViewController.h"

@interface YCResetPasswordViewController ()

@property (weak, nonatomic) IBOutlet UITextField *newpwd;
@property (weak, nonatomic) IBOutlet UITextField *confirmNewPassword;

@end

@implementation YCResetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"找回登录密码";
    [self.newpwd addTarget:self action:@selector(newpwdChanged:) forControlEvents:UIControlEventEditingChanged];
    [self.confirmNewPassword addTarget:self action:@selector(newpwdChanged:) forControlEvents:UIControlEventEditingChanged];
}

-(void)newpwdChanged:(UITextField *)textFeild{
    if (textFeild.text.length > 19) {
        textFeild.text = [textFeild.text substringToIndex:19];
        [[UIApplication sharedApplication].keyWindow presentMessageTips:@"你的输入超过最大长度"];
        [textFeild resignFirstResponder];
    }
}


#pragma mark - 充值密码
- (IBAction)resetClick:(id)sender {
    
    if (!_newpwd.text.length) {
        [[UIApplication sharedApplication].keyWindow presentFailureTips:@"请输入新密码"];
        return;
    }
    if (!_confirmNewPassword.text.length) {
        [[UIApplication sharedApplication].keyWindow presentFailureTips:@"请输入新密码"];
        return;
    }
    if (![YCCommonTool validatePassword:self.newpwd.text])
    {
        [[UIApplication sharedApplication].keyWindow presentFailureTips:@"请勿输入特殊字符"];
        return;
    }
    if (![self.newpwd.text isEqualToString:self.confirmNewPassword.text])
    {
        [[UIApplication sharedApplication].keyWindow presentFailureTips:@"两次输入的密码不一致"];
        return;
    }
    
    NSDictionary *params = @{@"member_mobile":self.phoneNum,//手机号
                             @"member_pwd":_newpwd.text//密码
                             };
    
    [[YCLoginManager shareManager] resetPassword:params complete:^(BOOL success, NSString *message) {
        
        if (success == YES) {
            
            [YCLoginManager shareManager].phoneNumber = [self.phoneNum cleanTelephoneNumber];
            
            [[UIApplication sharedApplication].keyWindow presentMessageTips:@"重置密码成功"];
            AccountPasswordLoginViewController *accountVC = nil;
            for (UIViewController *vc in self.navigationController.viewControllers) {
                if ([vc isKindOfClass:[AccountPasswordLoginViewController class]]) {
                    accountVC = (AccountPasswordLoginViewController *)vc;
                    break;
                }
            }
            if (accountVC) {
                [self.navigationController popToViewController:accountVC animated:YES];
            } else {
                
                accountVC = [[AccountPasswordLoginViewController alloc]init];
                [self showViewController: accountVC sender:nil];
            }
            
        }else{
            
            [[UIApplication sharedApplication].keyWindow presentMessageTips:@"重置密码失败"];
        }
    }];


}

- (void)dealloc {
    [self clearKeyboardCorverNotificationAndGesture];
    [self tapGestureHandel];
}


@end
